package com.liuda.workbench.controller;

import com.liuda.commons.constants.Constants;
import com.liuda.commons.domain.ReturnObject;
import com.liuda.commons.utils.DateUtil;
import com.liuda.commons.utils.UUIDUtil;
import com.liuda.settings.domain.DicValue;
import com.liuda.settings.domain.User;
import com.liuda.settings.service.DicValueService;
import com.liuda.settings.service.UserService;
import com.liuda.workbench.domain.Activity;
import com.liuda.workbench.domain.CAR;
import com.liuda.workbench.domain.Clue;
import com.liuda.workbench.domain.ClueRemark;
import com.liuda.workbench.service.ActivityService;
import com.liuda.workbench.service.CARService;
import com.liuda.workbench.service.ClueRemarkService;
import com.liuda.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ClueController {
    @Autowired
    ClueService clueService;
    @Autowired
    UserService userService;
    @Autowired
    DicValueService dicValueService;
    @Autowired
    ClueRemarkService clueRemarkService;
    @Autowired
    ActivityService activityService;
    @Autowired
    CARService carService;
    @RequestMapping("/workbench/clue/toIndex.do")
    public String toIndex(HttpServletRequest request){
        List<User> userList=userService.getAllUnlockedUsers();
        List<DicValue> appellationList=dicValueService.queryDicValueByTypeCode("appellation");
        List<DicValue> stateList=dicValueService.queryDicValueByTypeCode("clueState");
        List<DicValue> sourceList=dicValueService.queryDicValueByTypeCode("source");
        request.setAttribute("userList",userList);
        request.setAttribute("appellationList",appellationList);
        request.setAttribute("stateList",stateList);
        request.setAttribute("sourceList",sourceList);
        return "workbench/clue/index";
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/queryClueByConditions.do")
    public Object queryClueByConditions(Clue clue, Integer pageNo, Integer rowsPerPage){
        Integer startPage=(pageNo-1)*rowsPerPage;
        int count= clueService.queryClueCountByCondition(clue);
        List<Clue> clueList=clueService.queryClueByConditions(clue,startPage,rowsPerPage);
        Map map=new HashMap();
        map.put("totalRows",count);
        map.put("clueList",clueList);
        return map;
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/createClue.do")
    public Object createClue(Clue clue, HttpSession session){
        ReturnObject object=new ReturnObject();
        User user=(User)session.getAttribute(Constants.SESSION_USER);
        clue.setId(UUIDUtil.createId());
        clue.setCreateBy(user.getId());
        clue.setCreateTime(DateUtil.formatDateTime(new Date()));
        int count=clueService.insertClueSelective(clue);
        if(count<1){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试!");
        }else{
            object.setState(Constants.RETURN_OBJECT_SUCCESS);
        }
        return object;
    }
    @RequestMapping("/workbench/clue/toDetail.do")
    public String toDetail(String clueId,HttpServletRequest request){
        Clue clue=clueService.queryForDetailById(clueId);
        List<ClueRemark> clueRemarkList=clueRemarkService.queryForDetailByClueId(clueId);
        List<Activity> activityList=activityService.queryActivityByClueId(clueId);
        request.setAttribute("clue",clue);
        request.setAttribute("clueRemarkList",clueRemarkList);
        request.setAttribute("activityList",activityList);
        return "workbench/clue/detail";
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/createClueRemark.do")
    public Object createClueRemark(String noteContent,String clueId,HttpSession session){
        ClueRemark clueRemark=new ClueRemark();
        User user=(User)session.getAttribute(Constants.SESSION_USER);
        clueRemark.setNoteContent(noteContent);
        clueRemark.setClueId(clueId);
        clueRemark.setId(UUIDUtil.createId());
        clueRemark.setCreateBy(user.getId());
        clueRemark.setCreateTime(DateUtil.formatDateTime(new Date()));
        clueRemark.setEditFlag(Constants.CLUE_REMARK_EDIT_FLAG_FALSE);
        int count=clueRemarkService.insertOneSelective(clueRemark);
        ReturnObject object=new ReturnObject();
        if(count<1){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试！");
        }else {
            object.setState(Constants.RETURN_OBJECT_SUCCESS);
            object.setRetData(clueRemark);
        }
        return object;
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/saveBoundActivities.do")
    public Object saveBoundActivities(String[] ids,String clueId){
        List<CAR> carList=new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            CAR car=new CAR();
            car.setId(UUIDUtil.createId());
            car.setClueId(clueId);
            car.setActivityId(ids[i]);
            carList.add(car);
        }
        int count= carService.insertList(carList);
        ReturnObject object=new ReturnObject();
        if(count<1){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试...");
        }else{
            object.setState(Constants.RETURN_OBJECT_SUCCESS);
            List<Activity> activityList= activityService.selectByIdsForDetail(ids);
            object.setRetData(activityList);
        }
        return object;
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/releaseActivity.do")
    public Object releaseActivity(String activityId,String clueId){
        int count=carService.deleteByActivityIdAndClueId(activityId,clueId);
        ReturnObject object=new ReturnObject();
        if(count<1){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试...");
        }else{
            object.setState(Constants.RETURN_OBJECT_SUCCESS);
        }
        return object;
    }
    @RequestMapping("/workbench/clue/toConvert.do")
    public String toConvert(String clueId,HttpServletRequest request){
        Clue clue=clueService.queryForDetailById(clueId);
        List<DicValue> stages= dicValueService.queryDicValueByTypeCode("stage");
        List<Activity> activityList = activityService.selectByClueIdForDetail(clueId);
        request.setAttribute("clue",clue);
        request.setAttribute("stages",stages);
        request.setAttribute("activityList",activityList);
        return "workbench/clue/convert";
    }

    @ResponseBody
    @RequestMapping("/workbench/clue/convert.do")
    public Object convert(@RequestParam Map<String,Object> map,HttpSession session){
        User user=(User) session.getAttribute(Constants.SESSION_USER);
        ReturnObject object=new ReturnObject();
        try {
            clueService.convert(map,user);
            object.setState(Constants.RETURN_OBJECT_SUCCESS);
            object.setMsg("线索转换成功！");
        } catch (Exception e) {
            e.printStackTrace();
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙,请稍后重试...");
        }
        return object;
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/getClueById.do")
    public Object getClueById(String clueId){
        Clue clue=clueService.queryById(clueId);
        return clue;
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/editClue.do")
    public Object editClue(Clue clue){
        ReturnObject object=new ReturnObject();
        int count=clueService.updateById(clue);
        if(count<1){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试...");
        }else{
            object.setState(Constants.RETURN_OBJECT_SUCCESS);
            object.setMsg("修改成功！");
        }
        return object;
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/deleteClueByIds.do")
    public Object deleteClueByIds(String[] ids){
        ReturnObject object=new ReturnObject();
        int count=clueService.deleteByIds(ids);
        if(count<1){
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试...");
        }else{
            object.setState(Constants.RETURN_OBJECT_SUCCESS);
            object.setMsg("删除成功！");
        }
        return object;
    }
}
